import request from "./request";
import type { ResDataType } from "./request";

type SearchOption = {
  keyword: string;
  isStar: boolean;
  isDelected: boolean;
  page: number;
  pageSize: number;
};

//获取单个问卷信息
export const getQuestions = async (id: string): Promise<ResDataType> => {
  const url = `/api/question/${id}`;
  return (await request.get(url)) as ResDataType;
};
//创建问卷
export const creatQuestion = async (): Promise<ResDataType> => {
  const url = `/api/question/`;
  return (await request.post(url)) as ResDataType;
};
//获取(查询)问卷
export const getQuestionsList = async (
  option: Partial<SearchOption> = {}
): Promise<ResDataType> => {
  const url = "/api/question";
  return (await request.get(url, {
    params: option,
  })) as ResDataType;
};
//更新问卷
export const updateQuestion = async (
  id: string,
  opt: { [key: string]: any }
): Promise<ResDataType> => {
  const url = `/api/question/${id}`;
  return request.patch(url, opt) as ResDataType;
};
//复制问卷
export const copyQuestion = async (id: string): Promise<ResDataType> => {
  const url = `/api/question/${id}`;
  return request.post(url) as ResDataType;
};
//删除问卷
export const delectQuestion = async (ids: string[]): Promise<ResDataType> => {
  const url = `/api/question`;
  return request.delete(url, { data: { ids } }) as ResDataType;
};
//获取用户信息
export const getUserInfo = async (): Promise<ResDataType> => {
  const url = "/api/user/info";
  return request.get(url) as ResDataType;
};
//注册
export const register = async (
  username: string,
  password: string,
  nickname?: string
): Promise<ResDataType> => {
  const url = "/api/user/register";
  const params = { username, password, nickname: nickname || username };
  return request.post(url, params) as ResDataType;
};
//登录
export const login = async (
  username: string,
  password: string
): Promise<ResDataType> => {
  const url = "/api/user/login";
  const params = { username, password };
  return request.post(url, params) as ResDataType;
};
//获取统计页数据
export const getQuestionStatList = async (
  questionId: string,
  opt: { page: number; pageSize: number }
) => {
  const url = `/api/stat/${questionId}`;
  return (await request.get(url, { params: opt })) as ResDataType;
};
export const getComponentCount = async (
  questionId: string,
  componentId: string
) => {
  const url = `/api/stat/${questionId}/${componentId}`;
  return request.get(url) as ResDataType;
};
